<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-input
          v-model="queryParams.sex"
          placeholder="请输入性别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['model:personDemo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['model:personDemo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['model:personDemo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['model:personDemo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="personDemoList" @selection-change="handleSelectionChange" class="edit-table">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" >
          <template slot-scope="scope">
              <el-input placeholder="请输入内容" v-show="canDataEdit" v-model="scope.row.id" class="edit-table-input"></el-input>
              <span v-show="!canDataEdit">{{scope.row.id}}</span>
          </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name" >
          <template slot-scope="scope">
              <el-input placeholder="请输入内容" v-show="canDataEdit" v-model="scope.row.name" class="edit-table-input"></el-input>
              <span v-show="!canDataEdit">{{scope.row.name}}</span>
          </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex" >
          <template slot-scope="scope">
              <el-input placeholder="请输入内容" v-show="canDataEdit" v-model="scope.row.sex" class="edit-table-input"></el-input>
              <span v-show="!canDataEdit">{{scope.row.sex}}</span>
          </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="phone" >
          <template slot-scope="scope">
              <el-input placeholder="请输入内容" v-show="canDataEdit" v-model="scope.row.phone" class="edit-table-input"></el-input>
              <span v-show="!canDataEdit">{{scope.row.phone}}</span>
          </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" >
          <template slot-scope="scope">
              <el-input placeholder="请输入内容" v-show="canDataEdit" v-model="scope.row.address" class="edit-table-input"></el-input>
              <span v-show="!canDataEdit">{{scope.row.address}}</span>
          </template>
      </el-table-column>
      <el-table-column label="编码" align="center" prop="code" >
          <template slot-scope="scope">
              <el-input placeholder="请输入内容" v-show="canDataEdit" v-model="scope.row.code" class="edit-table-input"></el-input>
              <span v-show="!canDataEdit">{{scope.row.code}}</span>
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-save"
            @click="handleSave(scope.row)"
          >保存</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员模型示例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="form.sex" placeholder="请输入性别"/>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPersonDemo, getPersonDemo, delPersonDemo, addPersonDemo, updatePersonDemo } from "@/api/etl-model/personDemo";

export default {
  name: "PersonDemo",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      //是否可以编辑列表
      canDataEdit: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 人员模型示例表格数据
      personDemoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        name: null,
        sex: null,
        phone: null,
        address: null,
        code: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人员模型示例列表 */
    getList() {
      this.loading = true;
      listPersonDemo(this.queryParams).then(response => {
        this.personDemoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sex: null,
        phone: null,
        address: null,
        code: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员模型示例";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPersonDemo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人员模型示例";
      });
    },
    handleSave(row){
      updatePersonDemo(row).then(response => {
          this.msgSuccess("修改成功");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePersonDemo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPersonDemo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除人员模型示例编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPersonDemo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('model/personDemo/export', {
        ...this.queryParams
      }, `model_personDemo.xlsx`)
    }
  }
};
</script>
<style>
.edit-table .cell {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
    word-break: break-all;
    line-height: 23px;
    padding-left: 0px;
    padding-right: 0px;
}

.edit-table th, .edit-table td {
    padding: 0px 0;
}

.edit-table{
table-collapse:collapse;
border-top: 1px solid #DDD;
border-left: 1px solid #DDD;
}
.edit-table td {
    border-right: 1px solid #CCC;
    border-bottom: 1px solid #CCC;
}
.edit-table .el-input__inner{
    border: 0px solid #DCDFE6;
}
</style>
